/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package tarea;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

/**
 *
 * @author Gabriel Bran
 */
public class FactoryObject {

    private String sFileName = "";
    private FileOutputStream fos = null;
    private FileInputStream fis = null;
    private ObjectInputStream ois = null;
    private ObjectOutputStream oos = null;
    Object myObject = null;

    public void PersistObject(Object cdao) {
        try {
            if (cdao instanceof CDAO) {
                sFileName = "CDAO.dat";
            }
            fos = new FileOutputStream(sFileName);
            oos = new ObjectOutputStream(fos);
            oos.writeObject(cdao);
            oos.flush();
            oos.close();
        } catch (IOException e) {
            System.out.println(e.getMessage());
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
    }

    public Object CreateObject(Object cdao) {
        try {
            if (cdao instanceof CDAO) {
                sFileName = "CDAO.dat";
            }
            fis = new FileInputStream(sFileName);
            ois = new ObjectInputStream(fis);
            myObject = ois.readObject();
        } catch (IOException e) {
            System.out.println(e.getMessage());
            if (cdao instanceof CDAO) {
                myObject = new CDAO();
            }
            return myObject;
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
        return myObject;
    }
}
